package com.yf.exam.modules.user.exam.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.ApiRest;
import com.yf.exam.core.api.controller.BaseController;
import com.yf.exam.core.api.dto.PagingReqDTO;

import com.yf.exam.core.utils.excel.ExportExcel;
import com.yf.exam.modules.user.exam.dto.request.UserExamReqDTO;
import com.yf.exam.modules.user.exam.dto.response.UserExamRespDTO;
import com.yf.exam.modules.user.exam.service.UserExamService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;


/**
* <p>
* 考试记录控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-09-21 15:13
*/
@Api(tags={"考试记录"})
@RestController
@RequestMapping("/exam/api/user/exam")
public class UserExamController extends BaseController {

    @Autowired
    private UserExamService baseService;

    private HashMap<String,Integer> reqmap = new HashMap<>();


    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<UserExamRespDTO>> paging(@RequestBody PagingReqDTO<UserExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<UserExamRespDTO> page = baseService.paging(reqDTO);
        reqmap.put("current",reqDTO.getCurrent());
        reqmap.put("size",reqDTO.getSize());
        return super.success(page);
    }


    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/my-paging", method = { RequestMethod.POST})
    public ApiRest<IPage<UserExamRespDTO>> MyPaging(@RequestBody PagingReqDTO<UserExamReqDTO> reqDTO) {

        //分页查询并转换
        IPage<UserExamRespDTO> page = baseService.myPaging(reqDTO);

        return super.success(page);
    }


    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequestMapping(value = "/export")
    public ApiRest exportFile(HttpServletResponse response, @RequestBody UserExamReqDTO reqDTO) {
        // 导出文件名
        String fileName = "导出的成绩-" + System.currentTimeMillis() + ".xlsx";
        try {

            PagingReqDTO<UserExamReqDTO> req = new PagingReqDTO<>();

            //分页参数为空或者为0的话重新赋值
            if(!(null != reqmap.get("current") && reqmap.get("current") != 0) && !(null != reqmap.get("size") && reqmap.get("size") != 0)){
                reqmap.put("current",1);
                reqmap.put("size",10);
            }
            req.setCurrent(reqmap.get("current"));
            req.setSize(reqmap.get("size"));
            req.setParams(reqDTO);
            IPage<UserExamRespDTO> page = baseService.myPaging(req);
            int a = 5;
            int b = 500;
            boolean someCondition = true;
            int result = someCondition ? a : b;
            List<UserExamRespDTO> records = page.getRecords();
            for (UserExamRespDTO record : records) {
                record.setMaxScoreT(String.valueOf(record.getMaxScore()));
                record.setPassedT(record.getPassed() ? "已通过" : "未通过");
            }
            new ExportExcel("成绩", UserExamRespDTO.class).setDataList(records).write(response, fileName).dispose();
            return super.success();
        } catch (Exception e) {
            return failure(e.getMessage());
        }
    }


}
